#include "dlg_bookgs.h"
#include "ui_dlg_bookgs.h"

Dlg_BookGS::Dlg_BookGS(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Dlg_BookGS)
{
    ui->setupUi(this);
}

Dlg_BookGS::~Dlg_BookGS()
{
    delete ui;
}

void Dlg_BookGS::setBookId(int id)
{
    m_BookId = id;
}

void Dlg_BookGS::setType(bool is)
{
    m_isReturn = is;
}


void Dlg_BookGS::on_btn_get_clicked()
{

    do {
        auto strName = ui->lb_name->text();
        auto strPassword = ui->le_password->text();
        int userId = 0;
         //判断用户名密码是否正确
        bool ret = Sql_Mgr::getInstance()->login(strName, strPassword, userId);

        if(!ret)
        {
            this->done(0);
            break;
        }

        if(!m_isReturn)
        {
            Sql_Mgr::getInstance()->borrowBook(QString::number(userId), QString::number(m_BookId));
        }
        else
        {
            ui->btn_get->setText(QString("归还"));
            Sql_Mgr::getInstance()->returnBook(QString::number(userId), QString::number(m_BookId));
        }
        this->done(1);
    } while (false);
}


void Dlg_BookGS::on_btn_cancel_clicked()
{
    this->done(0);
}

